package com.water.control.util;


public class DataUtil {

    public static String byte2Hex(byte[] data) {
        final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
        if(data == null){
            return "";
        }
        final int length = data.length;
        if(length == 0){
            return "";
        }

        char[] hexChars = new char[data.length * 2];
        for ( int j = 0; j < data.length; j++ ) {
            int v = data[j] & 0xFF;
            hexChars[j * 2] = HEX_ARRAY[v >>> 4];
            hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
        }
        return new String(hexChars);
    }

    public static int hex2Int(String hex) {
        try {
            //1900000000,去掉16进制数末尾0
            hex = hex.replaceAll("(0)+$", "");
            return Integer.parseInt(hex, 16);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
